let routes =
    [
        {
            path: '/',
            name: 'layout',
            redirect: {name: 'index'},
            component: 'layout',
            children: [
                {
                    path: 'index',
                    name: 'index',
                    meta:{
                        title:'后台首页'
                    },
                    component: 'index/index',
                },
                {
                    path: 'shop/goods/list',
                    meta:{
                        title:'商品列表'
                    },
                    component: 'shop/goods/list'
                }
            ]
        },
        {
            path: '/login',
            name: 'login',
            meta:{
                title:'登录页'
            },
            component: 'login/index'
        },
        {
            path: '*',
            redirect: {name: 'index'}
        }
    ]

let getRoutes = function () {
    createRoute(routes)
    return routes
}

//去除结尾index
function getValue(str) {
    let index = str.lastIndexOf('/')
    let val = str.substring(index + 1, str.length)
    if (val === 'index') {
        return str.substring(index, -1)
    }
    return str
}

function createRoute(arr) {
    for (let i = 0; i < arr.length; i++) {
        if (!arr[i].component) return
        //去除结尾index
        let val = getValue(arr[i].component)
        //生成name
        arr[i].name = arr[i].name || val.replace(/\//g, '_')
        //生成path
        arr[i].path = arr[i].path || `/${val}`
        //生成component
        // let path1=arr[i].path
        let componentFun = import(/* webpackChunkName: "[request]" */ `../../views/${arr[i].component}.vue`)
        arr[i].component = () => componentFun
        if (arr[i].children && arr[i].children.length > 0) {
            createRoute(arr[i].children)
        }
    }
}

export default getRoutes()
